package com.sum.tea.rongrui

import android.content.Intent
import android.media.AudioManager
import android.media.SoundPool
import android.os.Bundle
import android.view.KeyEvent
import android.view.WindowManager
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_rongrui.RrReader
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.R
import com.sum.tea.databinding.ActivityConnect232Binding
import com.sum.tea.rongrui.viewmodel.ConnectViewModel

class Connect232Activity : BaseMvvmActivity<ActivityConnect232Binding, ConnectViewModel>() {
    override fun initView(savedInstanceState: Bundle?) {
        window.setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        initSound()
        checkAndRequestPermission()
        baud = 115200
        mBinding.baud57600.setOnClickListener { baud = 57600 }
        mBinding.baud115200.setOnClickListener { baud = 115200 }

        mBinding.textviewConnect.setOnClickListener {
            try {
                var result = RrReader.rrlib.Connect(devport, 57600, 1)
                if (result == 0) {
                    baud = 57600
                    mBinding.baud57600.isChecked = true
                    initRfid()
                    val intent = Intent().setClass(this@Connect232Activity, RMainActivity::class.java)
                    startActivity(intent)
                } else {
                    result = RrReader.rrlib.Connect(devport, 115200, 1)
                    if (result == 0) {
                        baud = 115200
                        mBinding.baud115200.isChecked = true
                        initRfid()
                        val intent = Intent().setClass(this@Connect232Activity, RMainActivity::class.java)
                        startActivity(intent)
                    } else {
                        ToastUtils.showShort(getString(R.string.openport_failed))
                    }
                }
            } catch (e: Exception) {
                ToastUtils.showShort(getString(R.string.openport_failed))
            }
        }
    }

    private fun initRfid() {
        val ReaderType = RrReader.rrlib.GetReaderType()
        val param = RrReader.rrlib.GetInventoryPatameter()
        if (ReaderType == 0x21 || ReaderType == 0x28 || ReaderType == 0x23 || ReaderType == 0x37 || ReaderType == 0x36) //R2000
        {
            param.Session = 1
        } else if (ReaderType == 0x70 || ReaderType == 0x71 || ReaderType == 0x31) //Ex10
        {
            param.Session = 254
        } else if (ReaderType == 0x61 || ReaderType == 0x63 || ReaderType == 0x65 || ReaderType == 0x66) //C6
        {
            param.Session = 1
        } else {
            param.Session = 0
        }
        RrReader.rrlib.SetInventoryPatameter(param)
    }

    private fun initSound() {
        soundPool = SoundPool(10, AudioManager.STREAM_MUSIC, 5)
        soundMap[1] = soundPool!!.load(this, R.raw.barcodebeep, 1)
        am = this.getSystemService(AUDIO_SERVICE) as AudioManager // 实例化AudioManager对象
        RrReader.rrlib.SetSoundID(soundMap[1]!!, soundPool)
    }

    override fun onResume() {
        super.onResume()
        RrReader.rrlib.PowerControll(null, true)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()

            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        RrReader.rrlib.PowerControll(null, false)
    }

    private fun checkAndRequestPermission() {
        PermissionUtils.permissionGroup(PermissionConstants.STORAGE)
            .callback(object : PermissionUtils.FullCallback {
                override fun onGranted(permissionsGranted: List<String>) {

                }

                override fun onDenied(permissionsDeniedForever: List<String>,
                                      permissionsDenied: List<String>) {

                }
            })
            .request()
    }

    companion object {
        private const val TAG = "COONECTRS232"
        private const val devport = "/dev/ttyS1" //RR
        private const val DEBUG = true
        @JvmField
		var baud: Int = 115200
        var soundMap: HashMap<Int, Int> = HashMap()
        private var soundPool: SoundPool? = null
        private const val volumnRatio = 0f
        private var am: AudioManager? = null
    }
}
